public class Leetcode1759 {

    public static void main(String[] args) {
        System.out.println(countHomogenous1("abbcccaa"));
        System.out.println(countHomogenous1("xy"));
        System.out.println(countHomogenous1("zzzzz"));
    }

    private static final int MOD = 1000000007;

    public static int countHomogenous(String s) {
        long res = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; ) {
            int end = i + 1;
            while (end < chars.length && chars[i] == chars[end]) {
                end++;
            }

            end -= i;
            res += (end * (end + 1L) / 2L) % MOD;
            i += end;
        }

        return (int) res;
    }

    public static int countHomogenous1(String s) {
        int res = 1;
        int next = 1;
        char[] chars = s.toCharArray();
        for (int i = 1; i < chars.length; i++) {
            res += chars[i] == chars[i - 1] ? next++ : 1;
            res %= MOD;
        }

        return res;
    }
}
